
*******************
* Collapsing HH outcomes for figure 1
* Siaya sample
* 2020-07-02
* last updated: 2020-07-16
* Magdalena Larreboure
*******************


set more off


  use "$dr/KEN1_fig1_hh.dta" , clear

 replace week=8 if week==9 | week==10 | week==. // there are very few obs in weeks 9 and 10 that we add to week 8
 replace week=11 if week == 12 // Round two data
 replace week=13 if week == 14
 
 g genderviolence=1 if gv1==1 | gv2==1 | gv3==1
 replace genderviolence=0 if gv1==0 & gv2==0 & gv3==0
 rename gv4 childrenbeaten
 rename gv3 gv_sexualacts


 gen baseline = cond(week == 1, 1, 0)
 gen baseline_week2 = cond(week == 2, 1, 0)
 
// - [ ] Food consumption c2_wins
// - [ ] Non-food consumption c3_wins
// - [ ] Missed meals adults f1_d_skippedadult_wins
// - [ ] Missed meals children f1_d_skippedchild_wins

 loc hhoutcomes c2_wins c3_wins f1_d_skippedadult_wins f1_d_skippedchild_wins genderviolence gv_sexualacts


foreach v of var `hhoutcomes' { // for saving labels before collapsing
	local l`v' : variable label `v'
        if `"`l`v''"' == "" {
 		local l`v' "`v'"	
  	}
	
	qui ttest `v', by(baseline)
	gen `v'_p = `r(p)'
	loc `v'_p = `r(p)'	
  }
  
foreach v of var childrenbeaten { // for saving labels before collapsing
	local l`v' : variable label `v'
        if `"`l`v''"' == "" {
 		local l`v' "`v'"	
  	}
	
	qui ttest `v', by(baseline_week2)
	gen `v'_p = `r(p)'
	loc `v'_p = `r(p)'	
  }
  
  
  foreach v of var `hhoutcomes' childrenbeaten {
    gen `v'_n = .

	forval i=1/15{
  qui sum `v' if week==`i'
  replace `v'_n=`r(N)' if week==`i'
  }
  }
  
collapse (mean) `hhoutcomes' childrenbeaten baseline (first) *_p *_n [aw = hh_weight], by(week)
sort week

// - [ ] Revenues e3_wins
// - [ ] Profits e4_wins
 preserve

 use "$dr/KEN1_fig1_ent.dta" , clear
 qui sum rev_feb_wins [aw = ent_weight]
 loc revfeb = `r(mean)'
 loc nrev = `r(N)'
 qui sum profits_feb_wins [aw = ent_weight]
 loc proffeb = `r(mean)'
//  keep week rev_wins profits_wins ent_weight
 drop if week==.
 loc ent rev_wins profits_wins
 foreach v of var `ent' { // for saving labels before collapsing
	local l`v' : variable label `v'
        if `"`l`v''"' == "" {
 		local l`v' "`v'"
  	}
	
	if "`v'"=="rev_wins" {
	qui ttest rev_wins == rev_feb_wins
	gen rev_wins_p = `r(p)'
	loc rev_wins_p = `r(p)'	
	}
	if "`v'"=="profits_wins" {
	qui ttest profits_wins == profits_feb_wins
	gen profits_wins_p = `r(p)'
	loc profits_wins_p = `r(p)'	
	}
	gen `v'_n = .
	
	forval i=1/15{
		qui sum `v' if week==`i'
		replace `v'_n=`r(N)' if week==`i'
  }
	
  }
  
  
 collapse (mean) rev_wins profits_wins (first) *_p *_n [aw = ent_weight], by(week)

 tempfile ent
 save `ent'
 
 set obs 11
 replace week=0 in 11
 sort week
  gen baseline = cond(week == 0, 1, 0)
 replace rev_wins=`revfeb' if week==0
 replace profits_wins=`proffeb' if week==0
 foreach var of varlist rev_wins profits_wins {
	
	gen aux = `var'
	replace `var' = `var'/aux[1] * 100 -100
	drop aux
	

}


foreach v of var rev_wins profits_wins { // attach saved labels
 	label var `v' "`l`v''"
  }
  g week_nent=532 if week==1
  replace week_nent=`nrev' if week==0
replace week_nent=451 if week==2
replace week_nent=434 if week==3
replace week_nent=720 if week==4
replace week_nent=884 if week==5
replace week_nent=916 if week==6
replace week_nent=312 if week==7
replace week_nent=571 if week==9
replace week_nent=603 if week==10
replace week=week+1
save "$da/forgraphs_ent_KEN1.dta", replace
 restore

 merge 1:1 week using `ent' , nogen
 
 // - [ ] Food prices 
//  - [ ] Non-food prices


preserve

 use "$dr/KEN1_fig1_prices.dta" , clear

keep if week<=15
// the collapse to categories by(week market) using weight_KLPS
collapse mean_price_1 [aw=weight_KLPS] ,  by(week market category)
// then collapse across markets (no weights)
collapse mean_price_1 ,  by(week category)
keep if inlist(category,"food", "non-food") 
egen np=group(category)
label de np 1 "food" 2 "non-food"
label values np
drop category
reshape wide mean_price_1 , i(week) j(np)
rename mean_price_11 foodpriceindex
rename mean_price_12 nonfoodpriceindex

keep week foodpriceindex nonfoodpriceindex
tempfile prices
save `prices'
restore

 merge 1:1 week using `prices' , nogen


 loc outcomes c2_wins c3_wins rev_wins profits_wins f1_d_skippedadult_wins f1_d_skippedchild_wins genderviolence gv_sexualacts childrenbeaten foodpriceindex nonfoodpriceindex 


 replace childrenbeaten=childrenbeaten[2] in 1
 loc pointmissing = (childrenbeaten[1] + childrenbeaten[3])/2
 replace childrenbeaten=`pointmissing' in 2
 replace childrenbeaten_n=childrenbeaten_n[2] in 1
 replace childrenbeaten_n=0 in 2


 foreach var of varlist `outcomes' {
	gen aux = `var'
	replace `var' = `var'/aux[1] * 100 -100
	drop aux
	label var `var' "`l`var''"
}


g week_n=745 if week==1
replace week_n=743 if week==2
replace week_n=1478 if week==3
replace week_n=1145 if week==4
replace week_n=1499 if week==5
replace week_n=986 if week==6
replace week_n=1215 if week==7
replace week_n=782 if week==8
replace week_n=1394 if week==11
replace week_n=1444 if week==13
replace week_n=611 if week==15



* ---------------- *


* ---------------- Salim from here down *

preserve
keep week c2_wins c3_wins genderviolence childrenbeaten c2_wins_p c3_wins_p ///
	genderviolence_p childrenbeaten_p c2_wins_n c3_wins_n genderviolence_n ///
	childrenbeaten_n

tempfile week1
save `week1'
restore	

preserve
keep week f1_d_skippedadult_wins f1_d_skippedchild_wins ///
	f1_d_skippedadult_wins_p f1_d_skippedchild_wins_p ///
	f1_d_skippedadult_wins_n f1_d_skippedchild_wins_n
replace week = week + 1

tempfile week2
save `week2'
restore

preserve
keep week foodpriceindex nonfoodpriceindex week_n
replace week = week + 2

tempfile week3
save `week3'
restore

use "$da/forgraphs_ent_KEN1.dta" , clear
replace week = week + 4

tempfile week5
save `week5'

clear all

set obs 16

gen week = _n - 1

merge 1:1 week using `week1', keep(match master) nogen
merge 1:1 week using `week2', keep(match master) nogen
merge 1:1 week using `week3', keep(match master) nogen
merge 1:1 week using `week5', keep(match master) nogen

gen rev_wins2 = .
replace rev_wins2 = 0 in 1
replace rev_wins2 = rev_wins[7] / 6 in 2
replace rev_wins2 = rev_wins[7] * 2 / 6 in 3
replace rev_wins2 = rev_wins[7] * 3 / 6 in 4
replace rev_wins2 = rev_wins[7] * 4 / 6 in 5
replace rev_wins2 = rev_wins[7] * 5 / 6 in 6
replace rev_wins2 = rev_wins[7] in 7

replace rev_wins = . in 6

gen profits_wins2 = .
replace profits_wins2 = 0 in 1
replace profits_wins2 = profits_wins[7] / 6 in 2
replace profits_wins2 = profits_wins[7] * 2 / 6 in 3
replace profits_wins2 = profits_wins[7] * 3 / 6 in 4
replace profits_wins2 = profits_wins[7] * 4 / 6 in 5
replace profits_wins2 = profits_wins[7] * 5 / 6 in 6
replace profits_wins2 = profits_wins[7] in 7

replace profits_wins = . in 6

*****

local varlist_1 c2_wins c3_wins genderviolence childrenbeaten c2_wins_p ///
	c3_wins_p genderviolence_p childrenbeaten_p c2_wins_n c3_wins_n ///
	genderviolence_n childrenbeaten_n

foreach var of local varlist_1 {

	replace `var' = `var'[9] + (`var'[12] - `var'[9]) / 3 in 10
	replace `var' = `var'[9] + (`var'[12] - `var'[9]) * 2 / 3 in 11
	replace `var' = `var'[12] + (`var'[14] - `var'[12]) / 2 in 13
	replace `var' = `var'[14] + (`var'[16]- `var'[14]) / 2 in 15
}

local varlist_2 f1_d_skippedadult_wins f1_d_skippedchild_wins ///
	f1_d_skippedadult_wins_p f1_d_skippedchild_wins_p ///
	f1_d_skippedadult_wins_n f1_d_skippedchild_wins_n

foreach var of local varlist_2 {

	replace `var' = `var'[10] + (`var'[13] - `var'[10]) / 3 in 11
	replace `var' = `var'[10] + (`var'[13] - `var'[10]) * 2 / 3 in 12
	replace `var' = `var'[13] + (`var'[15] - `var'[13]) / 2 in 14
}

local varlist_3 week_n

foreach var of local varlist_3 {

	replace `var' = `var'[11] + (`var'[14] - `var'[11]) / 3 in 12
	replace `var' = `var'[11] + (`var'[14] - `var'[11]) * 2 / 3 in 13
	replace `var' = `var'[14] + (`var'[16] - `var'[14]) / 2 in 15
}

local varlist_4 rev_wins profits_wins rev_wins_n profits_wins_n baseline ///
	week_nent

foreach var of local varlist_4 {

	replace `var' = `var'[13] + (`var'[15] - `var'[13]) / 2 in 14
}

drop if week > 11

save "$da/forgraphs_KEN1.dta", replace




